home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / python2.4 / test / test_signal.py < prev    next >
Text File  |  2005-10-18  |  2KB  |  74 lines

  1. # Test the signal module
  2. from test.test_support import verbose, TestSkipped, TestFailed
  3. import signal
  4. import os, sys, time
  5.  
  6. if sys.platform[:3] in ('win', 'os2') or sys.platform=='riscos':
  7.     raise TestSkipped, "Can't test signal on %s" % sys.platform
  8.  
  9. if verbose:
  10.     x = '-x'
  11. else:
  12.     x = '+x'
  13. pid = os.getpid()
  14.  
  15. # Shell script that will send us asynchronous signals
  16. script = """
  17.  (
  18.         set %(x)s
  19.         sleep 2
  20.         kill -HUP %(pid)d
  21.         sleep 2
  22.         kill -USR1 %(pid)d
  23.         sleep 2
  24.         kill -USR2 %(pid)d
  25.  ) &
  26. """ % vars()
  27.  
  28. def handlerA(*args):
  29.     if verbose:
  30.         print "handlerA", args
  31.  
  32. class HandlerBCalled(Exception):
  33.     pass
  34.  
  35. def handlerB(*args):
  36.     if verbose:
  37.         print "handlerB", args
  38.     raise HandlerBCalled, args
  39.  
  40. signal.alarm(20)                        # Entire test lasts at most 20 sec.
  41. hup = signal.signal(signal.SIGHUP, handlerA)
  42. usr1 = signal.signal(signal.SIGUSR1, handlerB)
  43. usr2 = signal.signal(signal.SIGUSR2, signal.SIG_IGN)
  44. alrm = signal.signal(signal.SIGALRM, signal.default_int_handler)
  45.  
  46. try:
  47.     os.system(script)
  48.  
  49.     print "starting pause() loop..."
  50.  
  51.     try:
  52.         while 1:
  53.             if verbose:
  54.                 print "call pause()..."
  55.             try:
  56.                 signal.pause()
  57.                 if verbose:
  58.                     print "pause() returned"
  59.             except HandlerBCalled:
  60.                 if verbose:
  61.                     print "HandlerBCalled exception caught"
  62.                 else:
  63.                     pass
  64.  
  65.     except KeyboardInterrupt:
  66.         if verbose:
  67.             print "KeyboardInterrupt (assume the alarm() went off)"
  68.  
  69. finally:
  70.     signal.signal(signal.SIGHUP, hup)
  71.     signal.signal(signal.SIGUSR1, usr1)
  72.     signal.signal(signal.SIGUSR2, usr2)
  73.     signal.signal(signal.SIGALRM, alrm)
  74.